#include "loadmapinstructionview.h"
#include "ui_loadmapinstructionview.h"
#include "../../programView/programview.h"

LoadMapInstructionView::LoadMapInstructionView(int styleId,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LoadMapInstructionView)
{
    ui->setupUi(this);
    initialDisplayStyle(styleId);
    
    ui->TabView_MapFile->setColumnCount(1);
    ui->TabView_MapFile->setColumnWidth(0,440); 
    QStringList tableHeadStr;
    tableHeadStr << tr("地图文件");
    ui->TabView_MapFile->setHorizontalHeaderLabels(tableHeadStr);
    
}

void LoadMapInstructionView::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        ui->TabView_MapFile->setGeometry(10,10,430,170);
        ui->JMP_LB_GBox->setGeometry(10,185,430,70);
        ui->note_GBox->setGeometry(10,260,430,70);

        ui->forward_Btn->setGeometry(10,350,70,40);
        ui->save_Btn->setGeometry(360,350,70,40);
        break;
    default:
        break;
    }
}

void LoadMapInstructionView::getInput()
{
    noteStr = ui->note_lineEdit->text();
    jmpLabel = ui->JMP_LB_SpinBox->text();
    if(ui->TabView_MapFile->currentColumn()>=0)//有时候ｆｔｐ可能获取失败，下面这行就会coredump
    {
         mapFileName = ui->TabView_MapFile->currentItem()->text();
    }

}

void LoadMapInstructionView::updateView()
{
    ui->JMP_LB_SpinBox->setValue(jmpLabel.toInt());
    ui->note_lineEdit->setText(noteStr);
}

void LoadMapInstructionView::changeLanguage(int language)
{
    if(1 == language)
    {
        translator.load("/agvwork/AgvHmi/baseBag/hmiProject/language/loadMapInstructionView.qm");
        qApp->installTranslator(&translator);
    }
    else
    {
        qApp->removeTranslator(&translator);
    }
}


void LoadMapInstructionView::on_save_Btn_clicked()
{
    getInput();
    emit finishedEditInstruction_signal();
    emit saveInstruction_signal(instructionStr);
}

void LoadMapInstructionView::on_forward_Btn_clicked()
{
    this->close();
    programView->instructionListFlag = 0;
    programView->editInstructionViewShowOrHide();
}

void LoadMapInstructionView::on_clearNote_Btn_clicked()
{
    ui->note_lineEdit->clear();
    emit cleanNote_signal();
}

void LoadMapInstructionView::listMapFileDirSlot(const QHash<QString, bool> &mapDirInfoList, const QString &ftpPath, const QString &clientPath)
{
    if(fileData.ftpOperateNum != EM_FTP_OPERATE_LOAD_MAP)
    {
        return;
    }
    else
    {
        // 清除之前的文件列表信息
        mapFileList.clear();
        QString tmpMapFile;
        // 将获取到的地图目录信息插入到地图文件列表中
        for(int i = 0; i < mapDirInfoList.keys().count(); i++)
        {
            if(!mapDirInfoList.values().at(i))
            {
                // 过滤掉.yaml格式外其他格式文件
                if(mapDirInfoList.keys().at(i).contains(".yaml"))
                {
                    tmpMapFile = mapDirInfoList.keys().at(i);
                    mapFileList << tmpMapFile.remove(QRegExp("\\s"));
                }
            }
        }

        // 将地图文件显示到控件上
        ui->TabView_MapFile->clear();
        ui->TabView_MapFile->setRowCount(mapFileList.count());
        QStringList tableHeadStr;
        tableHeadStr << tr("地图文件");
        ui->TabView_MapFile->setHorizontalHeaderLabels(tableHeadStr);

        for(int i = 0; i < mapFileList.count(); i++)
        {
            ui->TabView_MapFile->setItem(i, 0, new QTableWidgetItem(mapFileList.at(i)));
        }

        // 更新显示地图文件
        // 从容器中查找文件索引
        qDebug() << "Current File :" << mapFileName  << " Row = "  << mapFileList.indexOf(mapFileName,0);

        // 高亮显示
        ui->TabView_MapFile->selectRow(mapFileList.indexOf(mapFileName,0));
        ui->TabView_MapFile->setFocus();
        update();
        fileData.ftpOperateNum = EM_FTP_OPERATE_CLOSE;
    }
}
